package com.kehutong.material.service.impl;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import com.kehutong.material.entity.Question;
import com.kehutong.material.service.QuestionService;

/**
 * 问题库服务实现类
 * @author: liukunlong
 * @date: 2021-03-05 16:01
 */
@WebService("/material/question")
public class QuestionServiceImpl implements QuestionService{
    private static final Logger logger = LoggerFactory.getLogger(QuestionServiceImpl.class);

    @Inject
    private JdbcSession jdbcSession;

    /**
     * 根据条件获取问题库列表
     * @param jsonObject 查询对象
     * @return 问题库列表
     * @throws Exception 操作异常
     */
    @ReqMapping("/getQuestionList")
    public Page<Question> getQuestionList(JSONObject jsonObject) throws Exception {
        Page<Question> result = null;
        if (logger.isDebugEnabled()) {
            //logger.debug("parameter:" + jsonObject);
        }
        FindPage<Question> find = jdbcSession.findPage(Question.class)
                .eq(jsonObject, "id", "pid")
                .eq("deleted", 0)
                .between("createTime", jsonObject, "beginTime", "endTime")
                .like(jsonObject, "name", "answer");
        Integer pageSize = jsonObject.getInteger("pageSize");
        if (pageSize == null) {
            jsonObject.put("pageSize",Short.MAX_VALUE);
        }
        find.page(jsonObject);

        result = find.exe();
        if (logger.isDebugEnabled()) {
            logger.debug("result:" + result);
        }
        return result;
    }

    /***
     * 根据问题库标志获取问题库信息
     * @param id 问题库标志
     * @return 问题库对象
     * @throws Exception 操作异常
     */
    @ReqMapping("/getQuestionById")
    public Question getQuestionById(String id) throws Exception {
        Question result = null;
        if (logger.isDebugEnabled()) {
            logger.debug("parameter:" + id);
        }
        result = jdbcSession.findById(Question.class, id);
        if (logger.isDebugEnabled()) {
            logger.debug("result:" + result);
        }
        return result;
    }


    /**
     * 保存问题库
     *
     * @param question 问题库对象
     * @return 新增条数
     * @throws Exception 操作异常
     */
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/saveQuestion")
    public Question saveQuestion(Question question) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("parameter:" + question);
        }
        jdbcSession.insert(question);
        if (logger.isDebugEnabled()) {
            logger.debug("result:" + question);
        }
        return question;
    }


    /**
     * 更新问题库
     *
     * @param question 问题库对象
     * @return 问题库对象
     * @throws Exception 操作异常
     */
    @Bean(copy = true)
    @ReqMapping("/updateQuestion")
    public Question updateQuestion(Question question) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("parameter:" + question);
        }
        jdbcSession.updateById(question);
        if (logger.isDebugEnabled()) {
            logger.debug("result:" + question);
        }
        return question;
    }


    /***
     * 根据问题库标志删除问题库信息
     * @param id 问题库标志
     * @return 操作结果
     * @throws Exception 操作异常
     */
    @ReqMapping("/deleteQuestionById")
    public Boolean deleteQuestionById(String id) throws Exception {
        boolean flag = false;
        if (logger.isDebugEnabled()) {
            logger.debug("parameter:" + id);
        }
        if (!Objects.isEmpty(id)) {
            Question result = Pool.get(Question.class, id);
            if (result != null) {
                result.setDeleted(true);
                jdbcSession.updateById(result);
                flag = true;
            }
        }
        return flag;
    }

}
